// index.js
// 获取应用实例
const app = getApp()
const datautils = require("../../utils/datautil.js")
const timeUtil = require("../../utils/timeUtil")
const fs = wx.getFileSystemManager() // 文件管理器API
const wxAudio = wx.createInnerAudioContext({}) // 音频API
Page({
  data: {
    images: [],
    imagesSecond: [],
    trans: [],
    daution: 1000
  },

  onLoad() {
    this.transData()
    this.startAnim()
    this.saveJoinDays()
  },

  transData() {
    var originData = datautils.getData(0).concat(datautils.getData(2))
      .concat(datautils.getData(3)).concat(datautils.getData(4));
    for (var i = 0; i <= originData.length; i++) {
      var random = Math.floor(Math.random() * originData.length);
      var random2 = Math.floor(Math.random() * originData.length);
      this.data.images.push(originData[random]);
      this.data.imagesSecond.push(originData[random2]);
      if (originData.length - i > 6) {
        var tra = {
          translateX: -i * 50
        }
        this.data.trans.push(tra)
      }
    }
    for (var i = originData.length - 6; i > 0; i--) {
      var tra = {
        translateX: -i * 50
      }
      this.data.trans.push(tra)
    }

    this.setData({
      images: this.data.images,
      imagesSecond: this.data.imagesSecond
    })
  },

  startAnim() {
    this.animate('#anim', this.data.trans, this.data.trans.length * 1000, function () {
      this.startAnim()
    }.bind(this))
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  jumpToWords() {
    wx.navigateTo({
      url: '../wordsvoice/wordsvoice',
    })
  },
  jumpToPoem() {
    wx.navigateTo({
      url: '../poem/poem',
    })
  },
  jumpToEdit() {
    wx.navigateTo({
      url: '../edit/edit',
    })
  },
  jumpToSetting() {
    wx.navigateTo({
      url: '../setting/setting',
    })
  },
  jumpToChat() {
    wx.navigateTo({
      url: '../chat/chat',
    })
  },
  jumpToMore() {
    wx.showModal({
      title: '敬请期待',
      content: '更多功能，敬请期待'
    })
  },
  jumpToPersonal() {
    wx.navigateTo({
      url: '../personal/personal',
    })
  },
  //发送字符到XF
  clickImage(event) {
    var item = event.currentTarget.dataset.item;
    var text = item.chinese + "," + item.chinese + "," + item.english;
    const target = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.mp3`
    // var url = 'https://api.oick.cn/txt/apiz.php?text=' + text + '&spd=' + app.globalData.audioSpeed;
    var url = 'https://yuanxiapi.cn/api/voice/?text=' + text+'&spd='+app.globalData.audioSpeed;
    console.log(url)
    wx.request({
      url: url,
      method: 'GET',
      responseType: 'arraybuffer',
      success(result) {
        console.log(result)
        if (result.statusCode == 200 || result.statusCode == 201) {
          if (result.data != null) {
            fs.writeFile({
              filePath: target,
              data: result.data,
              encoding: 'binary',
              success: (res) => {
                wxAudio.src = target
                console.log("success")
              }
            })
            // onCanplay监听音频进入可以播放状态的事件
            wxAudio.onCanplay(() => {
              console.log("onCanplay")
              wxAudio.play()
            })
          }
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  saveJoinDays(){
    console.log("saveJoinDays")
    wx.getStorage({
      key: 'joinDays',
      success(res) {
        console.log(res.data+"ddd")
        if(res.data==null||res.data==""){
          wx.setStorage({
            key: "joinDays",
            data: timeUtil.Jh_getTimeStamp()
          });
        }
      },
      fail(res){
        wx.setStorage({
          key: "joinDays",
          data: timeUtil.Jh_getTimeStamp()
        });
      }
    })
  }
})